import { message } from 'antd';
import router from 'next/router';
import smartfetch from '@mmstudio/an000058';
import IconButton from './iconbutton';
import confirm from '../atoms/confirm';

export default function DelButton({ dlgtitle, query, api }: { dlgtitle?: string; query: Record<string, string>; api: string; }) {
	return <>
		<IconButton img='/icons/lzqshanchu.png' label='删除' onClick={async () => {
			const ok = await confirm('删除', '确认删除?');
			if (ok) {
				const data = await smartfetch<{ ok: true } | { ok: false; message: string; }, Record<string, string>>(api, 'delete', query);
				if (data.ok === true) {
					message.success({ content: '操作成功' });
					router.reload();
				} else {
					message.error({ content: data.message });
				}
			}
		}} />
		<style jsx>{`
.footer{
	display: flex;
	justify-content: center;
}
`}</style>
	</>;
}
